import 'package:flutter/material.dart';
import 'package:flutter_demo/utils/TokenManager.dart';
import 'package:fluttertoast/fluttertoast.dart';

class SettingPage extends StatefulWidget {
  const SettingPage({super.key});

  @override
  State<SettingPage> createState() => _SettingPageState();
}

class _SettingPageState extends State<SettingPage> {
  loginOut() async {
    Fluttertoast.showToast(msg: '退出登录');
    await TokenManager().setToken('');
    Navigator.pushNamed(context, '/login');
  }

  showExitDialog() {
    showDialog(
      context: context,
      barrierDismissible: true,
      builder: (context) {
        return AlertDialog(
          content: Text('确定要退出登录？'),
          actions: [
            TextButton(
              onPressed: () {
                loginOut();
                Navigator.of(context).pop();
              },
              child: Text('确定'),
            ),
            TextButton(
              onPressed: () {
                Navigator.of(context).pop();
              },
              child: Text('取消'),
            ),
          ],
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('系统设置'),
        centerTitle: true,
        leading: GestureDetector(
          onTap: () {
            Navigator.pop(context);
          },
          child: Image.asset(
            'assets/images/icon_arrow_left.png',
            width: 20,
            height: 20,
          ),
        ),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        children: <Widget>[
          Padding(
            padding: EdgeInsets.all(20),
            child: Container(
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(10),
              ),
              child: Padding(
                padding: EdgeInsets.all(10),
                child: Column(
                  children: [
                    Row(
                      children: [
                        Text('换绑手机', style: TextStyle(fontSize: 25)),
                        Expanded(child: Spacer()),
                        Image.asset(
                          'assets/images/icon_arrow_right.png',
                          width: 30,
                          height: 30,
                        ),
                      ],
                    ),
                    SizedBox(height: 10),
                    Divider(
                      height: 0.5,
                      color: const Color.fromARGB(255, 192, 189, 189),
                    ),
                    SizedBox(height: 10),
                    Row(
                      children: [
                        Text('修改密码', style: TextStyle(fontSize: 25)),
                        Expanded(child: Spacer()),
                        Image.asset(
                          'assets/images/icon_arrow_right.png',
                          width: 30,
                          height: 30,
                        ),
                      ],
                    ),
                    SizedBox(height: 10),
                    Divider(
                      height: 0.5,
                      color: const Color.fromARGB(255, 192, 189, 189),
                    ),
                    SizedBox(height: 10),
                    Row(
                      children: [
                        Text('消息通知设置', style: TextStyle(fontSize: 25)),
                        Expanded(child: Spacer()),
                        Image.asset(
                          'assets/images/icon_arrow_right.png',
                          width: 30,
                          height: 30,
                        ),
                      ],
                    ),
                    SizedBox(height: 10),
                    Divider(
                      height: 0.5,
                      color: const Color.fromARGB(255, 192, 189, 189),
                    ),
                    SizedBox(height: 10),
                    Row(
                      children: [
                        Text('修改密码', style: TextStyle(fontSize: 25)),
                        Expanded(child: Spacer()),
                        Image.asset(
                          'assets/images/icon_arrow_right.png',
                          width: 30,
                          height: 30,
                        ),
                      ],
                    ),
                    SizedBox(height: 10),
                    Divider(
                      height: 0.5,
                      color: const Color.fromARGB(255, 192, 189, 189),
                    ),
                    SizedBox(height: 10),
                    Row(
                      children: [
                        Text('清理缓存', style: TextStyle(fontSize: 25)),
                        Expanded(child: Spacer()),
                        Image.asset(
                          'assets/images/icon_arrow_right.png',
                          width: 30,
                          height: 30,
                        ),
                      ],
                    ),
                    SizedBox(height: 10),
                  ],
                ),
              ),
            ),
          ),

          SizedBox(height: 200),

          GestureDetector(
            onTap: () {
              showExitDialog();
            },
            child: Container(
              width: MediaQuery.of(context).size.width - 20,

              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(10),
              ),
              child: Column(
                children: [
                  SizedBox(height: 10),
                  Text(
                    '退出',
                    textAlign: TextAlign.center,
                    style: TextStyle(fontSize: 25, color: Colors.red),
                  ),

                  SizedBox(height: 10),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}
